package com.zzyl.nursing.service.impl;

import cn.hutool.json.JSONUtil;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description UserServiceImpl
 * @Author anru
 * @Date 2025-02-28 21:39
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 查询健康数据
     *
     * @param iotId
     * @return
     */
    @Override
    public AjaxResult queryServiceProperties(String iotId) {
        String  deviceDataListJson =(String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, iotId);
        List<DeviceData> deviceDataList = JSONUtil.toList(deviceDataListJson, DeviceData.class);
        List<Map<String, Object>> list = new ArrayList<>();
        deviceDataList.forEach(deviceData -> {
            Map<String, Object> map = new HashMap<>();
            map.put("functionId",deviceData.getFunctionId());
            map.put("value",deviceData.getDataValue());
            map.put("eventTime",deviceData.getAlarmTime());
            list.add(map);
        });
        return AjaxResult.success(list);
    }
}
